/*
 * MobilePhone.java
 *
 * This class is used to store mobile phone numbers and send a messsages to
 * one of of numbers store in the array.
 *
 * 
 */

package com.javacore.day9.phone;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * MobilePhone class performs various operations on an array and shows how
 * errors can be conveyed to the user using exception handling code.
 * @author 
 */
public class MobilePhone {
    
    /**
     * Instance variable to store the mobile numbers.
     */
    int phoneNumbers[];
    
    /**
     * Instance variable to store the message text.
     */
    String message;
    
    /**
     * Creates a new instance of MobilePhone
     */
    public MobilePhone() {
        message = "";
    }
    
    /**
     * Method to construct an array and add numbers to it.
     * If the user enters a negative or a character value, the exception is
     * caught and control is passed back to main method.
     */
    public void setPhoneNumbers() {
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("Enter the number of mobile numbers to store:");
            int size  = input.nextInt();
            phoneNumbers = new int[size];
            
            for(int index = 0;index < phoneNumbers.length;index++) {
                System.out.println("Enter a phone number:");
                phoneNumbers[index] = input.nextInt();
            }
        } catch(NegativeArraySizeException e) {
            System.out.println("Exception occurred - " +
                    "Array size should be a positive value.");
        } catch(InputMismatchException e) {
            System.out.println("Exception occurred - Data type mismatch." +
                    " Enter non-zero numeric value and try again.");
        } catch(Exception e) {
            System.out.println("Exception occurred - " + e.getMessage());
        }
    }
    
    /**
     * Method to display the mobile numbers stored in the database(array).
     */
    public void getPhoneNumbers() {
        
        System.out.println("The mobile phone database consists of following " +
                "phone numbers:");
        for(int index = 0;index < phoneNumbers.length;index++) {
            System.out.println(index + ". " + phoneNumbers[index]);
        }
    }
    
    /**
     * Method to send a message to a mobile number based on the user input.
     * Exception is caught for an invalid index, non-numeric values,
     * and an empty array.
     *
     */
    public void sendMessage() {
        Scanner input = new Scanner(System.in);
        try {
            getPhoneNumbers();
            System.out.println("Enter the index of phone number to which you" +
                    " want to send the message:");
            int index  = input.nextInt();
            System.out.println("Enter the message text: ");
            
            // Accept message text until the use press the Enter key
            input.useDelimiter("\n");
            this.message = input.next();
            System.out.printf("\nSending message [%s] to [%d] ..... please " +
                    "wait\n", this.message, this.phoneNumbers[index]);
            System.out.printf("\nMessage successfully sent.");
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception occurred - Invalid index.");
        } catch(InputMismatchException e) {
            System.out.println("Exception occurred - Data type mismatch. " +
                    "Check the data type and try again");
        } catch(NullPointerException e) {
            System.out.println("Exception occurred - The database of mobile " +
                    "numbers is empty.");
        } catch(Exception e) {
            System.out.println("Exception occurred - " + e.getMessage());
        }
    }
}
